
#include "stdafx.h"
#include "../client/DCPlusPlus.h"
#include "Resource.h"

#include "ClientsPage.h"
#include "ClientProfileDlg.h"
#include "ChangeRawCheatDlg.h"

#include "../client/SettingsManager.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

PropPage::TextItem ClientsPage::texts[] = {
	{ IDC_MOVE_CLIENT_UP, ResourceManager::MOVE_UP },
	{ IDC_MOVE_CLIENT_DOWN, ResourceManager::MOVE_DOWN },
	{ IDC_ADD_CLIENT, ResourceManager::ADD },
	{ IDC_CHANGE_CLIENT, ResourceManager::SETTINGS_CHANGE },
	{ IDC_REMOVE_CLIENT, ResourceManager::REMOVE },
	{ 0, ResourceManager::SETTINGS_AUTO_AWAY }
};

LRESULT ClientsPage::onInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	PropPage::translate((HWND)(*this), texts);

	CRect rc;

	ctrlProfiles.Attach(GetDlgItem(IDC_CLIENT_ITEMS));
	ctrlProfiles.GetClientRect(rc);

	ctrlProfiles.InsertColumn(0, CTSTRING(SETTINGS_NAME), LVCFMT_LEFT, rc.Width() / 3, 0);
	ctrlProfiles.InsertColumn(1, CTSTRING(COMMENT), LVCFMT_LEFT, rc.Width() / 2, 1);
	ctrlProfiles.InsertColumn(2, CTSTRING(ACTION), LVCFMT_LEFT, rc.Width() / 6, 0);

	ctrlProfiles.SetExtendedListViewStyle(LVS_EX_INFOTIP | (BOOLSETTING(FULL_ROW_SELECT) ? LVS_EX_FULLROWSELECT : 0));

	// Do specialized reading here
	ClientProfile::List lst = ClientProfileManager::getInstance()->getClientProfiles();

	for(ClientProfile::Iter i = lst.begin(); i != lst.end(); ++i) {
		ClientProfile& cp = *i;	
		addEntry(cp, ctrlProfiles.GetItemCount());
	}
	//Sulan start 33
	SetDlgItemText(IDC_PROFILE_COUNT, (TSTRING(PROFILE_COUNT) + _T(": ") + Util::toString(ctrlProfiles.GetItemCount())).c_str());
	//Sulan start svn51
	SetDlgItemText(IDC_PROFILE_VERSION, (TSTRING(PROFILE_VERSION) + _T(": ") + ClientProfileManager::getInstance()->getProfileVersion()).c_str());
	//End

	return TRUE;
}

LRESULT ClientsPage::onAddClient(WORD , WORD , HWND , BOOL& ) {
	ClientProfileDlg dlg;
	dlg.currentProfileId = -1;
	//Sulan start svn77
	dlg.itemCount = ctrlProfiles.GetItemCount();
	//End

	if(dlg.DoModal() == IDOK) {
		addEntry(ClientProfileManager::getInstance()->addClientProfile(
			dlg.name, 
			dlg.version, 
			dlg.tag, 
			dlg.extendedTag, 
			dlg.lock, 
			dlg.pk, 
			dlg.supports, 
			dlg.testSUR, 
			dlg.userConCom, 
			dlg.status,
			dlg.cheatingDescription,
			dlg.rawToSend, 
			dlg.useExtraVersion,
			dlg.checkMismatch,
			dlg.connection,
			dlg.comment,
			dlg.recheck,
			dlg.skipExtended,
			dlg.skipExtended2
			), ctrlProfiles.GetItemCount());
		
	}
	//Sulan start 33
	SetDlgItemText(IDC_PROFILE_COUNT, (TSTRING(PROFILE_COUNT) + _T(": ") + Util::toString(ctrlProfiles.GetItemCount())).c_str());
	//End
	return 0;
}

LRESULT ClientsPage::onChangeClient(WORD , WORD , HWND , BOOL& ) {
	if(ctrlProfiles.GetSelectedCount() == 1) {
		int sel = ctrlProfiles.GetNextItem(-1, LVNI_SELECTED);
		
		ClientProfileDlg dlg;

		dlg.currentProfileId = ctrlProfiles.GetItemData(sel);
		//Sulan start svn77
		dlg.itemCount = ctrlProfiles.GetItemCount();
		//End

		if(dlg.DoModal() == IDOK) {
			ctrlProfiles.SetItemText(dlg.currentProfileId, 0, dlg.name.c_str());
			ctrlProfiles.SetItemText(dlg.currentProfileId, 1, dlg.version.c_str());
			dlg.currentProfile.setName(dlg.name);
			dlg.currentProfile.setVersion(dlg.version);
			dlg.currentProfile.setTag(dlg.tag);
			dlg.currentProfile.setExtendedTag(dlg.extendedTag);
			dlg.currentProfile.setLock(dlg.lock);
			dlg.currentProfile.setPk(dlg.pk);
			dlg.currentProfile.setSupports(dlg.supports);
			dlg.currentProfile.setTestSUR(dlg.testSUR);
			dlg.currentProfile.setUserConCom(dlg.userConCom);
			dlg.currentProfile.setStatus(dlg.status);
			dlg.currentProfile.setCheatingDescription(dlg.cheatingDescription);
			dlg.currentProfile.setRawToSend(dlg.rawToSend);
//			dlg.currentProfile.setTagVersion(dlg.tagVersion);
			dlg.currentProfile.setUseExtraVersion(dlg.useExtraVersion);
			dlg.currentProfile.setCheckMismatch(dlg.checkMismatch);
			dlg.currentProfile.setConnection(dlg.connection);
			dlg.currentProfile.setComment(dlg.comment);
			dlg.currentProfile.setRecheck(dlg.recheck);
			dlg.currentProfile.setSkipExtended(dlg.skipExtended);
			dlg.currentProfile.setSkipExtended2(dlg.skipExtended2);
			ClientProfileManager::getInstance()->updateClientProfile(dlg.currentProfile);
		}
		ctrlProfiles.SetRedraw(FALSE);
		ctrlProfiles.DeleteAllItems();
		ClientProfile::List lst = ClientProfileManager::getInstance()->getClientProfiles();
		for(ClientProfile::Iter j = lst.begin(); j != lst.end(); ++j) {
			ClientProfile& cp = *j;	
			addEntry(cp, ctrlProfiles.GetItemCount());
		}
		ctrlProfiles.EnsureVisible(sel, FALSE);
		ctrlProfiles.SetRedraw(TRUE);
	}else if(ctrlProfiles.GetSelectedCount() > 1) {
		ChangeRawCheatDlg dlg;
		dlg.raw = 0;
		dlg.multiEdit = true;
		if(dlg.DoModal() == IDOK) {
			int selItem = -1;
			while((selItem = ctrlProfiles.GetNextItem(selItem, LVNI_SELECTED)) != -1) {
				ClientProfile nowEditing;
				ClientProfileManager::getInstance()->getClientProfile(ctrlProfiles.GetItemData(selItem), nowEditing);
				dcdebug("Now editing %s\n", nowEditing.getName().c_str());
				nowEditing.setRawToSend(dlg.raw);
				ClientProfileManager::getInstance()->updateClientProfile(nowEditing);
				ctrlProfiles.SetItemText(selItem, 2, RAWSETTING(nowEditing.getRawToSend()).c_str());
			}
		}
	}
	return 0;
}

LRESULT ClientsPage::onRemoveClient(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
	int i;
	while((i = ctrlProfiles.GetNextItem(-1, LVNI_SELECTED)) >= 0)
	{
		ClientProfileManager::getInstance()->removeClientProfile(ctrlProfiles.GetItemData(i));
		ctrlProfiles.DeleteItem(i);
		//Sulan start 33
		SetDlgItemText(IDC_PROFILE_COUNT, (TSTRING(PROFILE_COUNT) + _T(": ") + Util::toString(ctrlProfiles.GetItemCount())).c_str());
		//End
	}
	return 0;
}

LRESULT ClientsPage::onMoveClientUp(WORD , WORD , HWND , BOOL& ) {
	int i = ctrlProfiles.GetNextItem(-1, LVNI_SELECTED);
	if(i != -1 && i != 0 && ctrlProfiles.GetSelectedCount() == 1) {
		int n = ctrlProfiles.GetItemData(i);
		ClientProfileManager::getInstance()->moveClientProfile(n, -1/*, i*/);
		ctrlProfiles.SetRedraw(FALSE);
		ctrlProfiles.DeleteItem(i);
		ClientProfile cp;
		ClientProfileManager::getInstance()->getClientProfile(n, cp);
		addEntry(cp, i-1);
		ctrlProfiles.SetItemState(i-1, LVIS_SELECTED, LVIS_SELECTED);
		ctrlProfiles.EnsureVisible(i-1, FALSE);
		ctrlProfiles.SetRedraw(TRUE);
	}
	return 0;
}

LRESULT ClientsPage::onMoveClientDown(WORD , WORD , HWND , BOOL& ) {
	int i = ctrlProfiles.GetNextItem(-1, LVNI_SELECTED);
	if(i != -1 && i != (ctrlProfiles.GetItemCount()-1) && ctrlProfiles.GetSelectedCount() == 1) {
		int n = ctrlProfiles.GetItemData(i);
		ClientProfileManager::getInstance()->moveClientProfile(n, 1/*, i*/);
		ctrlProfiles.SetRedraw(FALSE);
		ctrlProfiles.DeleteItem(i);
		ClientProfile cp;
		ClientProfileManager::getInstance()->getClientProfile(n, cp);
		addEntry(cp, i+1);
		ctrlProfiles.SetItemState(i+1, LVIS_SELECTED, LVIS_SELECTED);
		ctrlProfiles.EnsureVisible(i+1, FALSE);
		ctrlProfiles.SetRedraw(TRUE);
	}
	return 0;
}

LRESULT ClientsPage::onReload(WORD , WORD , HWND , BOOL& ) {
	reload();
	return 0;
}

LRESULT ClientsPage::onInfoTip(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/) {
	int item = ctrlProfiles.GetHotItem();
	if(item != -1) {
		NMLVGETINFOTIP* lpnmtdi = (NMLVGETINFOTIP*) pnmh;
		ClientProfile cp;

		ClientProfileManager::getInstance()->getClientProfile(ctrlProfiles.GetItemData(item), cp);

		string infoTip = STRING(NAME) + ": " + cp.getName() +
			"\r\n" + STRING(COMMENT) + ": " + cp.getComment() +
			"\r\n" + STRING(CHEATING_DESCRIPTION) + ": " + cp.getCheatingDescription() +
			"\r\n" + STRING(RAW_COMMAND) + ": " + RAWSETTING(cp.getRawToSend());

		_tcscpy(lpnmtdi->pszText, infoTip.c_str());
	}
	return 0;
}

LRESULT ClientsPage::onContextMenu(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/) {
	RECT rc; 
	POINT pt; 
	GetCursorPos(&pt);
	ctrlProfiles.GetWindowRect(&rc);
	
	int item = ctrlProfiles.GetNextItem(-1, LVNI_SELECTED);
	if (PtInRect(&rc, pt) && item != -1 && ctrlProfiles.GetSelectedCount() == 1) {
		ChangeRawCheatDlg dlg;
		ClientProfile cp;
		
		ClientProfileManager::getInstance()->getClientProfile(ctrlProfiles.GetItemData(item), cp);
		dlg.name = cp.getName();
		dlg.cheatingDescription = cp.getCheatingDescription();
		dlg.raw = cp.getRawToSend();

		if(dlg.DoModal() == IDOK) {
			cp.setCheatingDescription(dlg.cheatingDescription);
			cp.setRawToSend(dlg.raw);
			ClientProfileManager::getInstance()->updateClientProfile(cp);
			ctrlProfiles.SetItemText(item, 2, RAWSETTING(cp.getRawToSend()).c_str());
		}
	}else if (PtInRect(&rc, pt) && item != -1 && ctrlProfiles.GetSelectedCount() > 1) {
		ChangeRawCheatDlg dlg;
		dlg.raw = 0;
		dlg.multiEdit = true;
		if(dlg.DoModal() == IDOK) {
			int selItem = -1;
			while((selItem = ctrlProfiles.GetNextItem(selItem, LVNI_SELECTED)) != -1) {
				ClientProfile nowEditing;
				ClientProfileManager::getInstance()->getClientProfile(ctrlProfiles.GetItemData(selItem), nowEditing);
				dcdebug("Now editing %s\n", nowEditing.getName().c_str());
				nowEditing.setRawToSend(dlg.raw);
				ClientProfileManager::getInstance()->updateClientProfile(nowEditing);
				ctrlProfiles.SetItemText(selItem, 2, RAWSETTING(nowEditing.getRawToSend()).c_str());
			}
		}
	}
	return 0;
}

void ClientsPage::reload() {
	ctrlProfiles.SetRedraw(FALSE);
	ctrlProfiles.DeleteAllItems();
	ClientProfile::List lst = ClientProfileManager::getInstance()->reloadClientProfiles();
	for(ClientProfile::Iter j = lst.begin(); j != lst.end(); ++j) {
		ClientProfile& cp = *j;	
		addEntry(cp, ctrlProfiles.GetItemCount());
	}
	//Sulan start svn51
	SetDlgItemText(IDC_PROFILE_VERSION, (TSTRING(PROFILE_VERSION) + _T(": ") + ClientProfileManager::getInstance()->getProfileVersion()).c_str());
	//End
	SetDlgItemText(IDC_PROFILE_COUNT, (TSTRING(PROFILE_COUNT) + _T(": ") + Util::toString(ctrlProfiles.GetItemCount())).c_str());
	ctrlProfiles.SetRedraw(TRUE);
}

void ClientsPage::addEntry(const ClientProfile& cp, int pos) {
	StringList lst;

	lst.push_back(cp.getName());
	lst.push_back(cp.getComment());
	lst.push_back(RAWSETTING(cp.getRawToSend()));
	ctrlProfiles.insert(pos, lst, 0, (LPARAM)cp.getId());
}

void ClientsPage::write() {
	ClientProfileManager::getInstance()->saveClientProfiles();
}
// iDC++
LRESULT ClientsPage::onCustomDraw(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/) {
	LPNMLVCUSTOMDRAW cd = (LPNMLVCUSTOMDRAW)pnmh;

	switch(cd->nmcd.dwDrawStage) {
	case CDDS_PREPAINT:
		return CDRF_NOTIFYITEMDRAW;

	case CDDS_ITEMPREPAINT:
		{
			try	{
				ClientProfile cp;
				ClientProfileManager::getInstance()->getClientProfile(ctrlProfiles.GetItemData(cd->nmcd.dwItemSpec), cp);
				if (!cp.getCheatingDescription().empty()) {
					cd->clrText = SETTING(BAD_CLIENT_COLOUR);
				}
				return CDRF_NEWFONT | CDRF_NOTIFYSUBITEMDRAW;
			}
			catch(const Exception&)
			{
			}
			catch(...) 
			{
			}
		}
		return CDRF_NOTIFYSUBITEMDRAW;

	default:
		return CDRF_DODEFAULT;
	}
}
// iDC++
/**
 * @file
 * $Id: ClientsPage.cpp 317 2005-08-23 18:55:13Z sulan $
 */